<template>
	<view class="feed-back">
		<view class="app-background-fixed"></view>
		<view class="cell app-pages">
			<view class="cell-item">
				<view class="cell-title">问题与建议</view>
				<u--textarea v-model="value" border="none" height="400rpx" count :maxlength="200"
					placeholder="任何使用问题和体验建议，都期望得到您的反馈，我们会尽快优化和提升"></u--textarea>
			</view>
			<view class="cell-item">
				<view class="cell-title">图片（选填，提供问题截图）</view>
				<view class="cell-image">
					<u-upload :fileList="fileList1" width="180rpx" height="180rpx" :divNumb="2" @afterRead="afterRead"
						@delete="deletePic" name="1" multiple :maxCount="10">
					</u-upload>
				</view>
			</view>
			<view class="cell-item">
				<view class="cell-title">联系电话</view>
				<u--textarea v-model="value" border="none" height="40rpx" placeholder="选填，便于我们与您联系"></u--textarea>
			</view>
		</view>
		<view class="button">
			<view class="submit" @click="handlerSubmit">提交</view>
		</view>
	</view>
</template>
<script>
	export default {
		name: 'feedBack',
		data() {
			return {
				value: '',
				fileList1: []
			}
		},
		methods: {
			handlerSubmit(){
				uni.showToast({
					title: '提交成功',
					icon: 'success'
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					resolve(url)
					// let a = uni.uploadFile({
					// 	url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
					// 	filePath: url,
					// 	name: 'file',
					// 	formData: {
					// 		user: 'test'
					// 	},
					// 	success: (res) => {
					// 		setTimeout(() => {
					// 			resolve(res.data.data)
					// 		}, 1000)
					// 	}
					// });
				})
			},
		},
	}
</script>
<style lang="scss" scoped>
	.feed-back {
		.button {
			box-sizing: border-box;
			padding-top: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			.submit {
				width: 240rpx;
				height: 60rpx;
				line-height: 60rpx;
				text-align: center;
				border: 2rpx solid #f74062;
				border-radius: 10rpx;
				color: #f74062;
			}
		}
		.cell-item {
			margin-top: 20rpx;
			box-shadow: 0px 0px 10rpx rgba(0, 0, 0, 0.1);
			background-color: #FFFFFF;
			padding: 20rpx 40rpx 30rpx 40rpx;
			box-sizing: border-box;
			border-radius: 15rpx;

			.cell-title {
				margin-bottom: 15rpx;
				box-sizing: border-box;
				padding: 10rpx 18rpx;
				font-size: 30rpx;
			}
			.cell-image{
				margin-top: 30rpx;
				box-sizing: border-box;
				padding: 0 18rpx;
			}
		}
	}
</style>
